﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zmc.Apparatu;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Zmc.Apparatu
{


    public class CreateApparatusTypeCommendHadnlers : IRequestHandler<CreateApparatusTypeCommends, ApiResult<int>>
    {

        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public CreateApparatusTypeCommendHadnlers(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 创建设备类型
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(CreateApparatusTypeCommends request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var data = mapper.Map<ApparatusType>(request);
            result.Data = await db.Insertable(data).ExecuteCommandAsync();
            if (result.Data > 0)
            {
                result.Code = ApiEnums.添加成功;
                result.Msg = "添加成功";

            }
            else
            {
                result.Code = ApiEnums.添加失败;
                result.Msg = "添加失败";
            }
            return result;
        }
    }
}
